package net.sudot.chess.business.wechat.message;

import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import net.sudot.chess.business.model.OrderRecharge;
import net.sudot.chess.business.model.User;
import net.sudot.chess.config.SystemProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 充值通知
 * <pre>
 * {{first.DATA}}
 *
 * {{accountType.DATA}}：{{account.DATA}}
 * 充值金额：{{amount.DATA}}
 * 充值状态：{{result.DATA}}
 * {{remark.DATA}}
 * <p/>
 * 最终发送示例:
 * <p/>
 * 您已成功充值320金豆
 *
 * 赠送金额：20金豆
 * 充值金额：300金豆
 * 充值状态：充值成功
 * 充值时间：2018-04-03 08:24
 *
 * 点击查看充值记录
 * </pre>
 *
 * @author tangjialin on 2018-04-02.
 */
@Component
public class RechargeTemplateMessage extends AbstractTemplateMessage {

    @Value("${system.config.wechat.rechargeTempleteId}")
    private String templateId;

    @Override
    public String getTemplateId() {
        return templateId;
    }

    @Override
    protected WxMpTemplateMessage.WxMpTemplateMessageBuilder builderTemplateMessage(User user, Object data, SystemProperties systemProperties) {
        OrderRecharge orderRecharge = (OrderRecharge) data;
        StringBuilder first = new StringBuilder("您已成功充值")
                .append(orderRecharge.getReceiveAmount().add(orderRecharge.getExchangeAmount()).toPlainString()).append("金豆");
        StringBuilder remark = new StringBuilder("充值时间：").append(formatDate(orderRecharge.getCreatedDate()))
                .append("\n\n点击查看充值记录");

        List<WxMpTemplateData> templateData = new ArrayList<>();
        templateData.add(new WxMpTemplateData("first", first.toString()));
        templateData.add(new WxMpTemplateData("accountType", "赠送金额"));
        templateData.add(new WxMpTemplateData("account", orderRecharge.getExchangeAmount().toPlainString() + "金豆"));
        templateData.add(new WxMpTemplateData("amount", orderRecharge.getReceiveAmount() + "金豆"));
        templateData.add(new WxMpTemplateData("result", "充值成功"));
        templateData.add(new WxMpTemplateData("remark", remark.toString()));
        WxMpTemplateMessage.WxMpTemplateMessageBuilder builder = WxMpTemplateMessage.builder().data(templateData);
        builder.url(systemProperties.getWebsite() + "/order/recharge/index");
        return builder;
    }

}
